package com.codingfuture.erp_system.service.impl;

import com.codingfuture.erp_system.dto.UserRoleDto;
import com.codingfuture.erp_system.entity.Emp;
import com.codingfuture.erp_system.mapper.UserRoleMapper;
import com.codingfuture.erp_system.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Emp> findByAll() {
        return userRoleMapper.findByAll();
    }

    @Override
    public List<UserRoleDto> readEmpRolesById(Integer id) {
        return userRoleMapper.readEmpRolesById(id);
    }

    @Override
    public List<UserRoleDto> readEmpRolesByAll() {
        return userRoleMapper.readEmpRolesByAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateEmpRoles(int id, Integer[] checkedStr) {
        int num = 0;
        int i = userRoleMapper.deleteById(id);
        for (Integer integer : checkedStr) {
            userRoleMapper.insert(id, integer);
            num++;
        }
        return num != 0;
    }
}
